//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package io.gitee.caoxiaoyu97.util;


import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class Convert {
    private static final List<String> TRUE_ARRAY = Arrays.asList("true", "yes", "y", "t", "ok", "1", "on", "是", "对", "真");

    public Convert() {
    }

    public static String like(String str) {
        return StringUtils.isEmpty(str) ? null : "%" + str + "%";
    }

    public static <T> void predicateNvl(T val, Consumer<T> consumer) {
        Predicate<T> predicate = ObjectUtils::isEmpty;
        Optional.ofNullable(val).filter(predicate.negate()).ifPresent(consumer);
    }

    public static String nvl(String val, String defaultVal) {
        return ObjectUtils.isEmpty(val) ? defaultVal : val;
    }

    public static <T, U> U nvl(T obj, Function<T, U> mapper) {
        return Optional.ofNullable(obj).map(mapper).orElse(null);
    }

    public static <T, U> U nvl(T obj, Function<T, U> mapper, U deftVal) {
        return Optional.ofNullable(obj).map(mapper).orElse(deftVal);
    }

    public static <T> T nvl(T val, T deftVal) {
        return ObjectUtils.isEmpty(val) ? deftVal : val;
    }

    public static String empty2null(String val) {
        return ObjectUtils.isEmpty(val) ? null : val;
    }

    public static BigDecimal nvl(BigDecimal val, BigDecimal defaultVal) {
        return val == null ? defaultVal : val;
    }

    public static BigDecimal null2Zero(BigDecimal val) {
        return nvl(val, BigDecimal.ZERO);
    }

    public static String null2ZeroString(BigDecimal val) {
        return nvl(val, BigDecimal.ZERO).toPlainString();
    }

    public static BigDecimal null2Zero(String val) {
        return ObjectUtils.isEmpty(val) ? null : new BigDecimal(val);
    }

    public static BigDecimal bigDecimal(String val) {
        return ObjectUtils.isEmpty(val) ? null : new BigDecimal(val);
    }

    public static int toInt(boolean value) {
        return value ? 1 : 0;
    }

    public static char toChar(boolean value) {
        return (char)toInt(value);
    }

    public static Character toCharacter(boolean value) {
        return toChar(value);
    }

    public static boolean toBoolean(String valueStr) {
        if (StringUtils.isNotBlank(valueStr)) {
            valueStr = valueStr.trim().toLowerCase();
            return TRUE_ARRAY.contains(valueStr);
        } else {
            return false;
        }
    }

    public static boolean isBoolean(Class<?> clazz) {
        return clazz == Boolean.class || clazz == Boolean.TYPE;
    }
}
